﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ORM.Business.Feedbacks;
using ORM.Business.Interfaces;
using ORM.Business.Models.Entities;
using ORM.Common.Logging;
using ORM.Resources;

namespace ORM.Business.Repositories
{
    public class ModelRepository : IModelRepository
    {
        public Feedback<IList<Model>> GetAllModel()
        {
            try
            {
                using (var entities = new OrmEntities())
                {
                    var models =
                        entities.Models.Where(t => !t.IsDeleted).OrderBy(t => t.Name).ToList();
                    return new Feedback<IList<Model>>(true, null, models);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return new Feedback<IList<Model>>(false, Messages.GeneralError);
            }
        }

        public Feedback<Model> InsertModel(Model t)
        {
            Model tnew = new Model
            {
                Name = t.Name,
                DeviceId = t.DeviceId,
                ManufacturerId = t.ManufacturerId,
                Description = t.Description,
                ImageUrl = t.ImageUrl,
                IsDeleted = false,
            };

            try
            {
                using (var entities = new OrmEntities())
                {
                    //Check for duplicate name
                    var exist = entities.Models
                        .FirstOrDefault(i => i.Name == tnew.Name && !i.IsDeleted);
                    if (exist != null)
                        return new Feedback<Model>(false, Messages.InsertCategory_DuplicateName);

                    entities.Models.Add(tnew);
                    entities.SaveChanges();                    
                }
                return new Feedback<Model>(true, Messages.InsertModelCategorySuccess, tnew);
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return new Feedback<Model>(false, Messages.GeneralError);
            }
        }

        public Feedback DeleteModelCategory(int id)
        {
            try
            {
                using (var entities = new OrmEntities())
                {                    
                    Model c = entities.Models.Single(i => i.Id == id);
                    c.IsDeleted = true;
                    foreach (var guide in entities.Guides.Where(i => i.ModelID == id).ToList())
                    {
                        guide.IsDeleted = true;
                    }
                    foreach (var question in entities.Questions.Where(i => i.ModelId == id).ToList())
                    {
                        question.IsDeleted = true;
                    }
                    entities.SaveChanges();

                    return new Feedback(true);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return new Feedback(false, Messages.GeneralError);
            }
        }

        public Feedback<Model> UpdateModelCategory(Model c)
        {
            try
            {
                using (var entities = new OrmEntities())
                {
                    // Check for duplicate name
                    Model exist = entities.Models.FirstOrDefault(i => i.Id != c.Id && i.Name == c.Name && !i.IsDeleted);
                    if (exist != null)
                        return new Feedback<Model>(false, Messages.InsertCategory_DuplicateName);
                    if (c.ImageUrl != null)
                    {
                        var category = entities.Models.Single(i => i.Id == c.Id);
                        category.Name = c.Name;
                        category.ManufacturerId = c.ManufacturerId;
                        category.DeviceId = c.DeviceId;
                        category.Description = c.Description;
                        category.ImageUrl = c.ImageUrl;
                        entities.SaveChanges();
                        return new Feedback<Model>(true, null, category);
                    }
                    else
                    {
                        var category = entities.Models.Single(i => i.Id == c.Id);
                        category.Name = c.Name;
                        category.ManufacturerId = c.ManufacturerId;
                        category.DeviceId = c.DeviceId;
                        category.Description = c.Description;
                       // category.ImageUrl = c.ImageUrl;
                        entities.SaveChanges();
                        return new Feedback<Model>(true, null, category);
                    }

                   
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return new Feedback<Model>(false, Messages.GeneralError);
            }
        }

        public Feedback<Model> GetModelCategoryDetails(int id)
        {
            try
            {
                using (var entities = new OrmEntities())
                {
                    Model category = entities.Models.SingleOrDefault(i => i.Id == id && !i.IsDeleted);
                    if (category != null)
                        return new Feedback<Model>(true, null, category);
                    else
                        return new Feedback<Model>(false, Messages.GetCategory_NotFound);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return new Feedback<Model>(false, Messages.GeneralError);
            }
        }
        //----------------------------------------------------------------------------------------------
    }
}